//
//  WYCommodityController.m
//  Hangbag
//
//  Created by admin on 2018/2/10.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCommodityController.h"
#import "WYTableView.h"
#import "YHContainerTableViewCell.h"
#import "YHSectionView.h"
#import "YHPosterTableViewCell.h"
#import "YHDescTableViewCell.h"
@interface WYCommodityController ()<UITableViewDelegate,UITableViewDataSource,YHDContainerCellDelegate>
@property (nonatomic, strong) WYTableView *tableView;
@property (nonatomic, assign) BOOL canScroll;
@property (nonatomic, strong) YHContainerTableViewCell *containerCell;
@property (nonatomic, strong) YHSectionView *sectionView;
@end

@implementation WYCommodityController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(void)setupUI
{
    [self initTopBarWithTitle:@"详情"];
    [self addLeftButton:[UIImage imageNamed:@"return"] width:0 height:0];
    [self addRightButton:[UIImage imageNamed:@"icon_share"] width:0 height:0];
    [self.view addSubview:self.tableView];
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"leaveTop" object:nil];
    [self setTabbarView];
}

-(void)setTabbarView
{
    UIView *tabView = [[UIView alloc]init];
    [self.view addSubview:tabView];
    [tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    tabView.backgroundColor=[UIColor redColor];
    UIView *collecView = [[UIView alloc]init];
    [tabView addSubview:collecView];
    UIView *leaseView = [[UIView alloc]init];
    [tabView addSubview:leaseView];
    UIView *purchaseView = [[UIView alloc]init];
    [tabView addSubview:purchaseView];
    
    [collecView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.right.equalTo(leaseView.mas_left).offset(0);
        make.width.mas_equalTo(leaseView);
        make.width.mas_equalTo(purchaseView);
    }];
    
    [leaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.equalTo(purchaseView.mas_left).offset(0);
        make.left.equalTo(collecView.mas_right).offset(0);
        make.width.mas_equalTo(collecView);
        make.width.mas_equalTo(purchaseView);
    }];
    
    [purchaseView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.top.bottom.mas_equalTo(0);
        make.left.equalTo(leaseView.mas_right).offset(0);
        make.width.mas_equalTo(collecView);
        make.width.mas_equalTo(leaseView);
        
    }];
    
//    collecView.backgroundColor = [UIColor purpleColor];
//    leaseView.backgroundColor = [UIColor yellowColor];
//    purchaseView.backgroundColor = [UIColor blueColor];
    
    UIButton *collecButton = [[UIButton alloc]init];
    [collecView addSubview:collecButton];
    [collecButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    UIButton *leaseButton = [[UIButton alloc]init];
    [leaseView addSubview:leaseButton];
    [leaseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    UIButton *purchaseButton = [[UIButton alloc]init];
    [purchaseView addSubview:purchaseButton];
    [purchaseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    
    [collecButton setTitle:@"放入收藏" forState:UIControlStateNormal];
    [collecButton setBackgroundColor:[UIColor hexStringToColor:@"fc655e" alpha:1.0]];
    [collecButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [leaseButton setTitleColor:[UIColor hexStringToColor:@"fc655e" alpha:1.0] forState:UIControlStateNormal];
    [leaseButton setTitle:@"马上租下" forState:UIControlStateNormal];
    [leaseButton setBackgroundColor:[UIColor hexStringToColor:@"ffffff" alpha:1.0]];
    
    [purchaseButton setTitle:@"立即买断" forState:UIControlStateNormal];
    [purchaseButton setTitleColor:[UIColor hexStringToColor:@"#fc655e" alpha:1.0] forState:UIControlStateNormal];
    [purchaseButton setBackgroundColor: [UIColor whiteColor]];
    
}



#pragma mark - Notification

- (void)changeScrollStatus {
    self.canScroll = YES;
    self.containerCell.objectCanScroll = NO;
}
#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        
        CGFloat offset = 64;
        if (iPhoneX) {
            offset = 88;
        }
        offset=0;
        CGFloat bottomCellOffset = [self.tableView rectForSection:1].origin.y - offset;
        bottomCellOffset = floorf(bottomCellOffset);
        if (scrollView.contentOffset.y >= bottomCellOffset) {
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
            if (self.canScroll) {
                self.canScroll = NO;
                self.containerCell.objectCanScroll = YES;
            }
        }else{
            //子视图没到顶部
            if (!self.canScroll) {
                scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
            }
        }
    }
}
#pragma mark - YHDContainerCellDelegate

- (void)mmtdOptionalScrollViewDidScroll:(UIScrollView *)scrollView {
    self.tableView.scrollEnabled = NO;
}

- (void)mmtdOptionalScrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger page = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
    [self.sectionView.segmentControl setSelectedSegmentIndex:page animated:YES];
    
    self.tableView.scrollEnabled = YES;
}

#pragma mark - UITableViewDelegate && UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return 1;
}

// 由于Demo中几个cell个数有限且全部不一样，所以这儿不用重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
    // 海报
     YHPosterTableViewCell *cell = [[YHPosterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"posterCell"];
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
    // 重点！横向滑动cell
    YHContainerTableViewCell *cell = [[YHContainerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"containerCell"];
    self.containerCell = cell;
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 300;
    }
    return self.view.frame.size.height - 64 -60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01f;
    }
    return 60.0f;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Init Views
- (WYTableView *)tableView {
    if (!_tableView) {
        _tableView = [[WYTableView alloc] initWithFrame:CGRectMake(0, 44+STATUSBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (YHSectionView *)sectionView {
    if (!_sectionView) {
        _sectionView = [[YHSectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        __weak typeof(self) weakSelf = self;
        [_sectionView.segmentControl setIndexChangeBlock:^(NSInteger index) {
            weakSelf.containerCell.isSelectIndex = YES;
            [weakSelf.containerCell.scrollView setContentOffset:CGPointMake(index*[UIScreen mainScreen].bounds.size.width, 0) animated:YES];
        }];
    }
    return _sectionView;
}
@end
